package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.cueb.wmis.mapper.AdminMapper;
import edu.cueb.wmis.mapper.GoodMapper;
import edu.cueb.wmis.mapper.IncomebillitemMapper;
import edu.cueb.wmis.pojo.Admin;
import edu.cueb.wmis.pojo.Good;
import edu.cueb.wmis.pojo.Incomebill;
import edu.cueb.wmis.mapper.IncomebillMapper;
import edu.cueb.wmis.pojo.Incomebillitem;
import edu.cueb.wmis.service.IIncomebillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.cueb.wmis.utils.AutoBillNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class IncomebillServiceImpl extends ServiceImpl<IncomebillMapper, Incomebill> implements IIncomebillService {

    @Autowired
    private IncomebillMapper incomebillMapper;
    @Autowired
    private IncomebillitemMapper incomebillitemMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Override
    public boolean addIncomeBill(Incomebill incomebill) {
        LocalDate date = LocalDate.now();
        //String name = incomebill.getAdmin().getName();
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetails userDetails = (UserDetails)principal;
        String username = userDetails.getUsername();
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", username));
        String year = ((Integer)LocalDate.now().getYear()).toString();
        Integer billCount = incomebillMapper.findMaxBillName(year);
        String billName = AutoBillNameUtils.getBillName(year,billCount);
        incomebill.setBillName(billName);
        incomebill.setInputTime(date);
        incomebill.setInputUserId(admin.getId());
        if(incomebillMapper.insert(incomebill) == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Incomebill> getIncomeBillAndItem() {
        List<Incomebill> incomebills = incomebillMapper.selectList(new QueryWrapper<>());
        for(Incomebill incomebill : incomebills){

            List<Incomebillitem> temp = new LinkedList<>();
            List<Incomebillitem> incomebillitems = incomebillitemMapper.selectList(new QueryWrapper<Incomebillitem>().eq("billId", incomebill.getId()));
            for(Incomebillitem incomebillitem : incomebillitems){
                Good goodInfo = goodMapper.getGoodInfo(incomebillitem.getGoodId());
                incomebillitem.setShelf(goodInfo.getShelf());
                incomebillitem.setHouse(goodInfo.getHouse());
                temp.add(incomebillitem);
            }
            //incomebill.setItems(incomebillitems);
            incomebill.setItems(temp);

        }
        return incomebills;
    }

    @Override
    public List<Incomebillitem> getIncomeItemsByBillId(Integer billId) {
        List<Incomebillitem> incomebillitems = null;
        try{
            incomebillitems = incomebillitemMapper.selectList(new QueryWrapper<Incomebillitem>().eq("billId", billId));

        }catch (Exception e){
            e.printStackTrace();
        }
        return incomebillitems;
    }

}
